package com.ele.client.activity.editor.widget;

import com.google.gwt.user.client.ui.FlowPanel;

public class WordsPanel extends FlowPanel {

    public void add(final IsEditorWidget widget) {
        add(widget.asWidget());

        ((WordEditorWidget) widget.asWidget()).addNavigationHandler(new NavigationEvent.Handler() {
            @Override
            public void onClick(NavigationEvent event) {
                NavigationEvent.Direction direction = event.getDirection();
                int index = getWidgetIndex(widget);
                WordEditorWidget target = null;
                if (direction == NavigationEvent.Direction.UP && index > 0) {
                    target = (WordEditorWidget) getWidget(index - 1);
                } else if (direction == NavigationEvent.Direction.DOWN && index < getWidgetCount() - 1) {
                    target = (WordEditorWidget) getWidget(index + 1);
                }
                if (target != null) {
                    target.setFocus(event.getFieldIndex(), true);
                }
            }
        });
    }
}
